#include <iostream>
#include "BlockQueue.hpp"
#include "Thread.hpp"


using namespace std;



void Consum(BlockQueue<int>& bq)
{
    while(1)
    {
        int data;
        bq.Pop(&data);//把数据从阻塞队列拿出来
        cout<<"Consumer get data:"<<data<<endl;
        sleep(1);
    }
}

void Product(BlockQueue<int>& bq)
{
    int cnt = 1;
    while(1)
    {
        bq.Enqueue(cnt);
        cout<<"Producter product data:"<<cnt<<endl;
        ++cnt;
    }
}


void StartComm(vector<Thread<BlockQueue<int>>>* threads,int nums, BlockQueue<int>& bq,func_t<BlockQueue<int>> func)
{
    for(int i = 0;i<nums;i++)
    {
        string name = "thread-" + to_string(i+1);
        threads->emplace_back(func,bq,name);
        threads->back().Strat();//创建线程
    }
}

void StratConsum(vector<Thread<BlockQueue<int>>>* threads,int nums, BlockQueue<int>& bq)
{
    StartComm(threads,nums,bq,Consum);
}

void StratProduct(vector<Thread<BlockQueue<int>>>* threads,int nums, BlockQueue<int>& bq)
{
    StartComm(threads,nums,bq,Product);
}

void WaitAllThread(vector<Thread<BlockQueue<int>>> threads)
{
    for(auto thread : threads)
    {
        thread.join();
    }
}


int main()
{
    BlockQueue<int>* bq = new BlockQueue<int>(5);//数据类型BlockQueue<int>
    vector<Thread<BlockQueue<int>>> threads;
    
    StratConsum(&threads,3,*bq);
    StratProduct(&threads,1,*bq);
    WaitAllThread(threads);
}